package com.iterator.service.web.promotionService;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iterator.bean.web.pojo.tcy.Goods;
import com.iterator.bean.web.pojo.zql.InfoData;
import com.iterator.bean.web.pojo.zql.GrouponRules;
import com.iterator.bean.web.pojo.zql.GrouponRulesExample;
import com.iterator.bean.web.zql.bo.GrouponRuleBO;
import com.iterator.bean.web.zql.bo.ListRecordBO;
import com.iterator.mapper.web.promotionMapper.GrouponRulesMapper;
import com.iterator.mapper.web.goodsManager.GoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author : L
 * @ProjectName : project-2
 * @Date : 1/9/2021
 * @Description :
 */
@Service
public class GrouponServiceImpl implements GrouponService {
    @Autowired
    GrouponRulesMapper grouponRulesMapper;
    @Autowired
    GoodsMapper goodsMapper;


    @Override
    public InfoData<GrouponRules> queryGrouponRuleList(ListRecordBO listRecordBO) {
        PageHelper.startPage(listRecordBO.getPage(), listRecordBO.getLimit());

        GrouponRulesExample grouponRulesExample = new GrouponRulesExample();
        grouponRulesExample.setOrderByClause(listRecordBO.getSort() + " " + listRecordBO.getOrder());
        GrouponRulesExample.Criteria criteria = grouponRulesExample.createCriteria();
        if (listRecordBO.getGoodsId() != null) {
            criteria.andGoodsIdEqualTo(listRecordBO.getGoodsId());
        }
        List<GrouponRules> grouponRules = grouponRulesMapper.selectByExample(grouponRulesExample);
        InfoData<GrouponRules> grouponRulesInfoData = new InfoData<>();
        grouponRulesInfoData.setItems(grouponRules);
        PageInfo<GrouponRules> grouponRulesPageInfo = new PageInfo<>(grouponRules);
        long total = grouponRulesPageInfo.getTotal();
        grouponRulesInfoData.setTotal((int) total);

        return grouponRulesInfoData;
    }

    @Override
    public GrouponRules insertGrouponRule(GrouponRuleBO grouponRuleBO) {
        Date date = new Date();
        Goods goods = goodsMapper.selectByPrimaryKey(grouponRuleBO.getGoodsId());
        GrouponRules grouponRules = new GrouponRules();

        grouponRules.setAddTime(date);
        grouponRules.setDiscount(grouponRuleBO.getDiscount());
        grouponRules.setDiscountMember(grouponRuleBO.getDiscountMember());
        grouponRules.setExpireTime(grouponRuleBO.getExpireTime());
        grouponRules.setGoodsId(grouponRuleBO.getGoodsId());
        grouponRules.setGoodsName(goods.getName());
        grouponRules.setPicUrl(goods.getPicUrl());

        grouponRulesMapper.insertSelective(grouponRules);
        return grouponRules;
    }

    @Override
    public Integer updateGrouponRule(GrouponRules grouponRules) {
        int i = grouponRulesMapper.updateByPrimaryKeySelective(grouponRules);
        return i;
    }

    @Override
    public Integer deleteGrouponRule(GrouponRules grouponRules) {
        grouponRules.setDeleted(true);
        int i = grouponRulesMapper.updateByPrimaryKeySelective(grouponRules);
        return i;
    }
}
